<?php

abstract class User
{

	static function GetAidByToken($token, $check = true)
	{
		if(empty($token)) return false;

		$crypt = new MyCrypt(CRYPT_KEY);
		$decrypted_token = $crypt->decrypt($token);
		$fields = explode(User::TOKEN_DELIM, $decrypted_token);
		if(3 != count($fields)) {
			return false;
		}

		list($admin_id, $utype, $login_time) = $fields;
		if($check) {
			$pdo = PDOWrapper::instance();

			$sql = "SELECT token FROM admins WHERE admin_id=:admin_id";
			$results = $pdo->query($sql, array('admin_id' => $admin_id));
			if(false === $results) {
				Utils::ShowError(ErrorDefine::DATABASE_ERROR);
				return false;
			} else if(empty($results)) {
				Utils::ShowError(ErrorDefine::USER_NOT_EXIST);
				return false;
			} else {
				$db_token = $results[0]['token'];
				if($db_token != $token) {
					return false;
				}
			}
		}
		$admin = array(
			'admin_id' => $admin_id,
			'admin_type' => $utype
		);
		return $admin;
	}

}

?>
